<template>
    <div>
        <div>
            <van-sticky v-if="model == 'form'">
                <block-index-nav-bar :title="'新建盘点'"></block-index-nav-bar>
            </van-sticky>
            <van-form v-show="model == 'form'" @submit="onSubmit">
                <van-field
                    v-model="name"
                    name="name"
                    label="盘点名称"
                    placeholder="盘点名称"
                    :rules="[{ required: true, message: '请填写盘点名称' }]"
                />
                <van-date-dopup
                    :name="'startDate'"
                    :label="'盘点开始时间'"
                    :placeholder="'选择时间'"
                    :msg="'请选择时间'"
                    v-model="startDate"
                />
                <van-date-dopup
                    :name="'endDate'"
                    :label="'盘点结束时间'"
                    :placeholder="'选择时间'"
                    :msg="'请选择时间'"
                    v-model="endDate"
                />
                <van-field
                    v-model="inventory_user"
                    rows="2"
                    label="盘点人员"
                    readonly="readonly"
                    name="inventory_user"
                    type="textarea"
                    @click="invenCli"
                    show-word-limit
                    :rules="[{ required: true, message: '请选择盘点人员' }]"
                />
                <van-field
                    v-model="equipment"
                    rows="2"
                    label="盘点种类"
                    readonly="readonly"
                    name="equipment"
                    type="textarea"
                    @click="equiCli"
                    show-word-limit
                />
                <van-field
                    v-model="depName"
                    rows="2"
                    label="盘点部门"
                    readonly="readonly"
                    name="depName"
                    type="textarea"
                    @click="depCli"
                    show-word-limit
                />
                <div style="margin: 16px;">
                    <van-button round block type="info" native-type="submit">
                        提交
                    </van-button>
                </div>
            </van-form>
        </div>
        <div v-if="model === 'user'">
            <user-checkbox-list :list="list" @rightCli="rightCli" :titleText="'选择人员'" :resultList="resultList"
                                @backMethod="backMethod"
                                :itemName="'true_name'"/>
        </div>
        <div v-if="model === 'equipment'">
            <user-checkbox-list :list="equipmentList" :titleText="'选择种类'" @rightCli="equiRightCli"
                                :resultList="equipmentResult"
                                @backMethod="backMethod"
                                :itemName="'name'"/>
        </div>
        <div v-if="model === 'dep'">
            <user-checkbox-list :list="depList" :titleText="'选择部门'" @rightCli="depRightCli" :resultList="depResult"
                                @backMethod="backMethod"
                                :itemName="'name'"/>
        </div>
    </div>
</template>

<script>
import vanDateDopup from "@/uitl/vanDateDopup";
import userCheckboxList from "@/components/userCheckboxList";
import {Config} from "@/config/formDataOptions";
import {API_URL} from "@/config/urlInfo";
import blockIndexNavBar from "@/components/blockIndexNavBar";


export default {
    name: "newInventory",
    components: {
        vanDateDopup,
        userCheckboxList,
        blockIndexNavBar
    },
    data() {
        return {
            model: "form",
            name: '',
            currentDate: new Date(),
            startDate: "",
            endDate: "",
            inventory_user: "",
            inventory_user_ids: [],
            list: [],
            resultList: [],
            equipmentList: [],
            equipmentResult: [],
            equipment: "",
            equipmentName: [],
            depName: "",
            depList: [],
            depResult: [],
            dep: []

        };
    },
    mounted() {
        this.onInit()
        this.onEquipmentInit()
        this.onDepInit()
    },
    methods: {
        onSubmit(values) {
            values.dep = this.dep
            values.equipmentName = this.equipmentName
            values.inventory_user_ids = this.inventory_user_ids
            Config.postDatas(API_URL.mobileInventorySave, values, res => {
                this.$router.go(-1)
                this.$toast("操作成功")
            }, this)
        },
        rightCli(result) {
            let str = ""
            this.inventory_user_ids.length = 0
            this.resultList = result
            result.forEach(item => {
                str += item.true_name + ","
                this.inventory_user_ids.push(item.id)
            })
            this.inventory_user = str
            this.model = 'form'
        },
        equiRightCli(result) {
            let str = ""
            this.equipmentResult = result
            this.equipmentName.length = 0
            result.forEach(item => {
                str += item.name + ","
                this.equipmentName.push(item.id)
            })
            this.equipment = str
            this.model = 'form'
        },
        depRightCli(result) {
            let str = ""
            this.depResult = result
            this.dep.length = 0
            result.forEach(item => {
                str += item.name + ","
                this.dep.push(item.id)
            })
            this.depName = str
            this.model = 'form'
        },
        invenCli() {
            this.model = 'user'
        },
        equiCli() {
            this.model = 'equipment'
        },
        depCli() {
            this.model = 'dep'
        },
        backMethod() {
            this.model = 'form'
        },
        onInit() {
            Config.postDatas(API_URL.mobileUserSearch, {}, res => {
                this.list = res.data
            }, this)
        },
        onEquipmentInit() {
            Config.postDatas(API_URL.mobileEquipmentList, {}, res => {
                this.equipmentList = res.data
            }, this)
        },
        onDepInit() {
            Config.postDatas(API_URL.mobileDepList, {}, res => {
                this.depList = res.data
            }, this)
        },
    },
}
</script>

<style scoped>

</style>